<?php
/**
 * 提现配置模型
 * 作者: 王勇程 (laughskydragon@qq.com)
 */

namespace app\common\model;

use app\common\model\ModelBase;

class WithdrawConfig extends ModelBase
{
    // 表名

    
    // 自动时间戳
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    
    // 字段类型转换
    protected $type = [
        'id' => 'integer',
        'min_amount' => 'float',
        'max_amount' => 'float',
        'fee_rate' => 'float',
        'fee_min' => 'float',
        'fee_max' => 'float',
        'daily_limit' => 'integer',
        'monthly_limit' => 'float',
        'auto_audit' => 'integer',
        'auto_audit_amount' => 'float',
        'status' => 'integer',
        'create_time' => 'integer',
        'update_time' => 'integer',
    ];
    
    /**
     * 获取提现配置
     */
    public function getConfig()
    {
        $config = $this->getInfo(['status' => 1]);
        
        if (!$config) {
            // 返回默认配置
            return [
                'min_amount' => 10.00,
                'max_amount' => 10000.00,
                'fee_rate' => 0.005,
                'fee_min' => 1.00,
                'fee_max' => 50.00,
                'daily_limit' => 3,
                'monthly_limit' => 50000.00,
                'auto_audit' => 0,
                'auto_audit_amount' => 1000.00,
                'work_days' => '1,2,3,4,5',
                'work_start_time' => '09:00',
                'work_end_time' => '18:00',
                'status' => 1
            ];
        }
        
        return $config;
    }
    
    /**
     * 计算手续费
     */
    public function calculateFee($amount)
    {
        $config = $this->getConfig();
        
        // 按比例计算手续费
        $fee = $amount * $config['fee_rate'];
        
        // 限制最小手续费
        if ($fee < $config['fee_min']) {
            $fee = $config['fee_min'];
        }
        
        // 限制最大手续费
        if ($fee > $config['fee_max']) {
            $fee = $config['fee_max'];
        }
        
        return round($fee, 2);
    }
    
    /**
     * 检查是否在工作时间
     */
    public function isWorkTime()
    {
        $config = $this->getConfig();
        
        // 检查工作日
        $currentWeekday = date('N'); // 1-7，周一到周日
        $workDays = explode(',', $config['work_days']);
        
        if (!in_array($currentWeekday, $workDays)) {
            return false;
        }
        
        // 检查工作时间
        $currentTime = date('H:i');
        $workStartTime = $config['work_start_time'];
        $workEndTime = $config['work_end_time'];
        
        return $currentTime >= $workStartTime && $currentTime <= $workEndTime;
    }
    
    /**
     * 检查金额是否符合要求
     */
    public function validateAmount($amount)
    {
        $config = $this->getConfig();
        
        if ($amount < $config['min_amount']) {
            return ['status' => false, 'message' => "提现金额不能少于{$config['min_amount']}元"];
        }
        
        if ($amount > $config['max_amount']) {
            return ['status' => false, 'message' => "提现金额不能超过{$config['max_amount']}元"];
        }
        
        return ['status' => true, 'message' => ''];
    }
    
    /**
     * 检查是否可以自动审核
     */
    public function canAutoAudit($amount)
    {
        $config = $this->getConfig();
        
        return $config['auto_audit'] == 1 && $amount <= $config['auto_audit_amount'];
    }
}